package GUI.ventas;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class PagoTarjeta extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel _pagoTarjeta;	

	private JPanel _panelUsuario;
	private JLabel labelUsuario;
	private JTextField txtUsuario;

	private JPanel _panelContra;
	private JLabel labelContra;
	private JTextField txtContra;
	
	private JPanel _panelTotal;
	private JLabel labelTotal;
	private JTextField txtTotal;

	private JScrollPane scrollPane;
	private JButton comprobarUsuario;
	private JButton finalizarCompra;

	private Compra compra;
	private int totalFinalRecibidoDeCompra;
	private String stringtotalFinalRecibidoDeCompra;
	
	private String usuario;
	private String password;

	public PagoTarjeta(Compra compra) {
		super();
		this.setTitle("PAGO EN EFECTIVO");
		this.setBounds(500, 100, 400, 500);
		this.compra = compra;
		totalFinalRecibidoDeCompra = this.compra.gettotalFinal();
		stringtotalFinalRecibidoDeCompra = Integer.toString(totalFinalRecibidoDeCompra);
		/**
		 * boton.setBounds(x, y, width, height)
		 */

		this.setVisible(true);
		this.setResizable(false);
		this.build();
		this.finalizarCompra();
		this.verificaDatos();
	}

	private void build() {
		this._pagoTarjeta = new JPanel(new GridLayout(5, 0));
		this.scrollPane = new JScrollPane(this._pagoTarjeta);
		this._pagoTarjeta.setBorder(new TitledBorder(new EtchedBorder(),
				"Pago a traves de Tarjeta"));

		this.add(scrollPane, BorderLayout.CENTER);
		
		/**
		 * 
		 */
		this._panelUsuario = new JPanel(new BorderLayout());
		//
		this.labelUsuario = new JLabel("Usuario =       ");
		this.labelUsuario.setFont(new Font("Serif", Font.BOLD, 20));
		this.labelUsuario.setForeground(Color.green);
		//
		this.txtUsuario = new JTextField(15);
		this.txtUsuario.setFont(new Font("Serif", Font.BOLD, 20));
		this.txtUsuario.setForeground(Color.green);
		this.txtUsuario.setEditable(true);
		//
		this._panelUsuario.add(this.labelUsuario,
				BorderLayout.WEST);
		this._panelUsuario.add(this.txtUsuario);
		//
		this._pagoTarjeta.add(this._panelUsuario,
				BorderLayout.CENTER);

		/**
		 * 
		 */

		this._panelContra = new JPanel(new BorderLayout());
		//
		this.labelContra = new JLabel("Contrase�a = " + "\t");
		this.labelContra.setFont(new Font("Serif", Font.BOLD, 20));
		//
		this.txtContra = new JTextField(15);
		this.txtContra.setFont(new Font("Serif", Font.BOLD, 20));
		this.txtContra.setText("");
		//
		this._panelContra.add(labelContra,
				BorderLayout.WEST);
		this._panelContra.add(txtContra);
		this._pagoTarjeta.add(_panelContra,
				BorderLayout.SOUTH);

		
		this._panelTotal = new JPanel(new BorderLayout());
		//
		this.labelTotal = new JLabel("TOTAL COMPRA =   ");
		this.labelTotal.setFont(new Font("Serif", Font.BOLD, 20));
		this.labelTotal.setForeground(Color.red);
		//
		this.txtTotal = new JTextField(15);
		this.txtTotal.setFont(new Font("Serif", Font.BOLD, 20));
		this.txtTotal.setForeground(Color.red);
		this.txtTotal.setText(stringtotalFinalRecibidoDeCompra);
		this.txtTotal.setEditable(false);
		//
		this._panelTotal.add(labelTotal, BorderLayout.WEST);
		this._panelTotal.add(txtTotal);
		//
		this._pagoTarjeta.add(_panelTotal, BorderLayout.NORTH);
		
		/**
		 * 
		 */

		this.comprobarUsuario = new JButton("COMPROBAR USUARIO");
		this.comprobarUsuario.setFont(new Font("Serif", Font.BOLD, 20));
		this._pagoTarjeta.add(comprobarUsuario, BorderLayout.WEST);
		this.comprobarUsuario.setIconTextGap(2);
		this.comprobarUsuario.setHorizontalTextPosition(SwingConstants.CENTER);
		this.comprobarUsuario.setVerticalTextPosition(SwingConstants.BOTTOM);
		
		
		
		/**
		 * 
		 */
		this.finalizarCompra = new JButton("FINALIZAR COMPRA");
		this.finalizarCompra.setFont(new Font("Serif", Font.BOLD, 20));
		this._pagoTarjeta.add(finalizarCompra, BorderLayout.EAST);
		this.finalizarCompra.setBackground(Color.LIGHT_GRAY);
		this.finalizarCompra.setForeground(Color.white);
		this.finalizarCompra.setIconTextGap(2);
		this.finalizarCompra.setHorizontalTextPosition(SwingConstants.CENTER);
		this.finalizarCompra.setVerticalTextPosition(SwingConstants.BOTTOM);
		this.finalizarCompra.setEnabled(false);
	}

	public void verificaDatos() {
		comprobarUsuario.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (comprobarContrasena(txtUsuario.getText(), txtContra.getText() )){
					
					JOptionPane.showMessageDialog(null, "Usuario y contrase�as correctas", "Correcto", JOptionPane.INFORMATION_MESSAGE);
					finalizarCompra.setEnabled(true);
				}
				else{
					JOptionPane.showMessageDialog(null, "Usuario o contrase�a incorrectas", "Incorrecto", JOptionPane.ERROR_MESSAGE);
					txtUsuario.setText("");
					txtContra.setText("");
				}				
			}
		});

	}

	public void actualizalabelContra(String s) {
		this.txtContra.setEditable(true);
		this.txtContra.setText(s);
	}

	public void finalizarCompra() {
		// TODO
		finalizarCompra.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				TicketDeCompra ticket = new TicketDeCompra(compra,usuario);
			}
		});
	}
	
	private boolean comprobarContrasena (String user, String contra){
		if (user.equalsIgnoreCase("admin") && contra.equals("1234"))
		{
			this.usuario = user;
			this.password = contra;
			return true;
		}
		else
			return false;		
	}
	
	public String getUsuario(){
		return this.usuario;
	}
	
	public String getPassword()
	{
		return this.password;
	}
}
